//
//  UtilitiesViewController.m
//  Finder
//
//  Created by cdm on 11-3-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UtilitiesViewController.h"
#import "ContentCell.h"
#import "BackgroundSettingViewController.h"
#import "LocalizationViewController.h"
#import "SectionViewController.h"
#import "HouseCosignViewController.h"

@interface UtilitiesViewController ()

- (void)appLanguageDidChange;
    
@end

@implementation UtilitiesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appLanguageDidChange) name:kAppLanguageChangedNotification object:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
    
    self.parentViewController.navigationItem.title = self.title;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else if (section == 1) {
        return 1;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    ContentCell *cell  = (ContentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    
    // Configure the cell...
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLabel.text = [AppLanguage getString:@"Background"]; 
        } else if (indexPath.row == 1) {
            cell.titleLabel.text = [AppLanguage getString:@"Language"];
        }
    } else if (indexPath.section == 1) {
        cell.titleLabel.text = [AppLanguage getString:@"Tools1"];
    } 
    
    [cell showLeftIcon:NO];
    
    UIImage *webImage = [UIImage imageNamed:@"ic_show_next.png"];
    cell.rightIconImageView.image = webImage;
    
    return cell;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            BackgroundSettingViewController *targetController = [[BackgroundSettingViewController alloc] initWithNibName:@"BackgroundSettingView" bundle:nil];
            [self.navigationController pushViewController:targetController animated:YES];
            [targetController release];
        } else if (indexPath.row == 1) {
            LocalizationViewController *targetController = [[LocalizationViewController alloc] initWithNibName:@"LocalizationView" bundle:nil];
            [self.navigationController pushViewController:targetController animated:YES];
            [targetController release];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            HouseCosignViewController *targetController = [[HouseCosignViewController alloc] initWithNibName:@"HouseCosignView" bundle:nil];
            [self.navigationController pushViewController:targetController animated:YES];
            [targetController release];
        }
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionViewController *controller = [[SectionViewController alloc] initWithNibName:@"SectionView" bundle:nil];
    [controller loadView];
    
    if (section == 0) {
        controller.titleLabel.text = [AppLanguage getString:@"Setting"];
    } else if (section == 1) {
        controller.titleLabel.text = [AppLanguage getString:@"Tools"];
    }
    
    UIView *view = controller.view;
    
    [controller release];
    
    return view;
}

#pragma mark - App Language
    
- (void)appLanguageDidChange
{   
    [self.tableView reloadData];
}
    
@end
